﻿/// Author:					Walter Ferrari
/// Created:				2010-01-18
/// ref:                    http://www.snello.it
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using InteractChartCommonLib;
using InteractChartLib;

namespace DataFilesViewer.Controls
{
    public class dfvMainView : Control, IdfvControl
    {
        internal TabControl _elementMainTab;
        internal const string ElementMainTabName = "MainTabElement";

        public event EventHandler TemplateApplied; 

        public dfvMainView()
        {
            this.DefaultStyleKey = typeof(dfvMainView);
        }

        /// <summary>
        /// Gets or sets the treeview element.
        /// </summary>
        /// <value>The treeview element.</value>
        private TabControl MainTabElement
        {
            get { return _elementMainTab; }
            set { _elementMainTab = value; }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            object mainTabObj = GetTemplateChild(ElementMainTabName);
            Debug.Assert(typeof(TabControl).IsInstanceOfType(mainTabObj) || (mainTabObj == null),
                "The template part ElementMainTabName is not an instance of TabControl!");
            MainTabElement = mainTabObj as TabControl;

            MainTabElement.SelectionChanged += OnMainTabSelectionChanged;
      
        }

        #region [ MainTabElement HANDLERS ]

        private void OnMainTabSelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        #endregion

        public void UpdateView(IFileDropFormat filedroppedObj, string selectedItem)
        {
            MainTabElement.Items.Clear();
            

            foreach (string subitem in filedroppedObj.GetListSubItems())
            {
                TabItem tbItem = new TabItem();
                tbItem.Header = subitem;
                
                //build content
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Vertical;
                //sp.Background = new SolidColorBrush(Colors.Red);
                sp.Margin = new Thickness(15, 15, 15, 15);
                sp.Width = MainTabElement.ActualWidth - 65 - (MainTabElement.Margin.Left + MainTabElement.Margin.Right);
                sp.Height = MainTabElement.ActualHeight - (MainTabElement.Margin.Top + MainTabElement.Margin.Bottom);
                sp.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                sp.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                
                
                dfvDatagrid myGrid = new dfvDatagrid();
                myGrid.TemplateApplied += InitializeDataGrid;
                myGrid.AttachDatasource(filedroppedObj, subitem);
                myGrid.Width = sp.Width;
                myGrid.Height = sp.Height / 2 - sp.Margin.Bottom*2;
                

                InteractChartLib.InteractChart myChart = new InteractChartLib.InteractChart();
                myChart.TemplateApplied += InitializeChart;
                myChart.AttachDatasource(filedroppedObj, subitem);
                myChart.Width = sp.Width;
                myChart.Height = sp.Height / 2 - sp.Margin.Top*2;

                sp.Children.Add(myChart);
                sp.Children.Add(myGrid);
                
                tbItem.Content = sp;
                
                MainTabElement.Items.Add(tbItem);
            }

        }

        void InitializeDataGrid(object sender, EventArgs e)
        {
            dfvDatagrid myGrid = (dfvDatagrid)sender;

            if (myGrid != null)
                myGrid.UpdateMainGrid();
        }

        void InitializeChart(object sender, EventArgs e)
        {
            InteractChart myChart = (InteractChart)sender;

            /*if (myChart != null)
                myChart.UpdateChart();*/
        }


    }
}
